package com.vm.activity;

import java.util.List;
import org.json.JSONObject;
import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.vm.commons.VMConst;
import com.vm.db.CollectionDao;
import com.vm.model.ActiveSoft;
import com.vm.model.Collection;
import com.vm.model.Soft;
import com.vm.myview.FoundDown;
import com.vm.utils.ApkUtils;
import com.vm.utils.JsonUtils;
import com.vm.utils.MyDisplayImageOptions;
import com.vm.utils.MyVolley;
import com.wwtscapp.apk.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

@SuppressLint("InflateParams")
public class CollectionActivity extends Activity {
	ListView listview;
	ImageView imgbtn_search_back;
	private CollectionDao cDao = null;
	private List<Collection> listcollection;
	TextView tv_uninstalled_number;
	TextView tv_installed_number;
	LinearLayout found_ll_1;
	LinearLayout found_ll_2;
	ImageView iv_nonecollection;
	RequestQueue mQueue;
	String packageName;
	public static Soft soft = new Soft();
	ActiveSoft activeSoft;
	int notInstall = 0;
	int hasInstall = 0;
	TextView tv_uninstalled;
	TextView tv_uninstalled1;
	TextView tv_uninstalled2;
	TextView tv_installed;
	TextView tv_installed1;
	TextView tv_installed2;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		cDao = new CollectionDao(CollectionActivity.this);
		listcollection = cDao.selectall();
		mQueue = MyVolley.getRequestQueue();
		if (listcollection.size() > 0) {
			setContentView(R.layout.collection);
			init();
		} else {
			setContentView(R.layout.collection_none);
			imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
			imgbtn_search_back.setOnClickListener(btn_backListener);
		}

	}

	// public void onResume(){
	// init();
	// super.onResume();
	// }
	public void init() {
		tv_uninstalled = (TextView) findViewById(R.id.tv_uninstalled);
		tv_uninstalled1 = (TextView) findViewById(R.id.tv_uninstalled1);
		tv_uninstalled2 = (TextView) findViewById(R.id.tv_uninstalled2);
		tv_installed = (TextView) findViewById(R.id.tv_installed);
		tv_installed1 = (TextView) findViewById(R.id.tv_installed1);
		tv_installed2 = (TextView) findViewById(R.id.tv_installed2);
		tv_uninstalled_number = (TextView) findViewById(R.id.tv_uninstalled_number);
		tv_installed_number = (TextView) findViewById(R.id.tv_installed_number);
		found_ll_1 = (LinearLayout) findViewById(R.id.found_ll_1);
		found_ll_2 = (LinearLayout) findViewById(R.id.found_ll_2);
		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
		imgbtn_search_back.setOnClickListener(btn_backListener);
		for (int i = 0; i < listcollection.size(); i++) {
			final int number = i;
			String softId = listcollection.get(i).getSoftId();
			String url = VMConst.DETAIL_URL + softId;
			JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url,
					null, new Listener<JSONObject>() {

						@Override
						public void onResponse(JSONObject arg0) {
							// TODO Auto-generated method stub
							soft = JsonUtils.jsonToBean(arg0, Soft.class);
							boolean isInstalled = ApkUtils.isAppInstalled(
									soft.getPackageName(),
									CollectionActivity.this);
							activeSoft = new ActiveSoft();
							activeSoft.setSoftId(soft.getId());
							activeSoft.setName(soft.getName());
							activeSoft.setImageUrl(soft.getImageUrl());
							activeSoft.setApkUrl(soft.getApkUrl());
							activeSoft.setSize(soft.getSize());
							activeSoft.setStar(soft.getStar());
							if (isInstalled) {
								tv_installed.setVisibility(View.VISIBLE);
								tv_installed1.setVisibility(View.VISIBLE);
								tv_installed2.setVisibility(View.VISIBLE);
								tv_installed_number.setVisibility(View.VISIBLE);
								found_ll_2.addView(dyAddView2(number,
										activeSoft, 1));
								hasInstall++;
								tv_installed_number.setText(Integer
										.toString(hasInstall));
								if (notInstall == 0) {
									tv_uninstalled.setVisibility(View.GONE);
									tv_uninstalled1.setVisibility(View.GONE);
									tv_uninstalled2.setVisibility(View.GONE);
									tv_uninstalled_number
											.setVisibility(View.GONE);
								} else {
									tv_uninstalled.setVisibility(View.VISIBLE);
									tv_uninstalled1.setVisibility(View.VISIBLE);
									tv_uninstalled2.setVisibility(View.VISIBLE);
									tv_uninstalled_number
											.setVisibility(View.VISIBLE);
								}

							} else {
								tv_uninstalled.setVisibility(View.VISIBLE);
								tv_uninstalled1.setVisibility(View.VISIBLE);
								tv_uninstalled2.setVisibility(View.VISIBLE);
								tv_uninstalled_number
										.setVisibility(View.VISIBLE);
								found_ll_1.addView(dyAddView1(number,
										activeSoft, 1));
								notInstall++;
								tv_uninstalled_number.setText(Integer
										.toString(notInstall));
								if (hasInstall == 0) {
									tv_installed.setVisibility(View.GONE);
									tv_installed1.setVisibility(View.GONE);
									tv_installed2.setVisibility(View.GONE);
									tv_installed_number
											.setVisibility(View.GONE);
								} else {
									tv_installed.setVisibility(View.VISIBLE);
									tv_installed1.setVisibility(View.VISIBLE);
									tv_installed2.setVisibility(View.VISIBLE);
									tv_installed_number
											.setVisibility(View.VISIBLE);
								}
							}
						}

					}, null);
			mQueue.add(jsonObjectRequest);
		}
	}

	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// Log.i(TAG, "imgbtn_search_back返回按钮监听");
			onBackPressed();

		}
	};

	/**
	 * 返回按钮监听(手机自带返回按钮)
	 */
	@Override
	public void onBackPressed() {
		// Log.i(TAG, "返回按钮监听(手机自带返回按钮)");
		super.onBackPressed();
		// System.out.println("按下了back键   onBackPressed()");
	}

	public LinearLayout dyAddView1(int i, final ActiveSoft activeSoft,
			int progress) {
		LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
				R.layout.collection_item, null);
		ImageView iv_softImage = (ImageView) linearLayout
				.findViewById(R.id.iv_softImage);
		TextView tv_softName = (TextView) linearLayout
				.findViewById(R.id.tv_softName);
		RatingBar rb_star = (RatingBar) linearLayout.findViewById(R.id.rb_star);
		TextView tv_softSize = (TextView) linearLayout
				.findViewById(R.id.tv_softSize);
		Button btn_download = (Button) linearLayout
				.findViewById(R.id.btn_download);
		if (activeSoft.getName() != null) {
			tv_softName.setText(activeSoft.getName());
		}
		if (activeSoft.getSize() != null) {
			tv_softSize.setText(Float.toString(activeSoft.getSize()) + "MB");
		}
		String uri = VMConst.URL + activeSoft.getImageUrl();
		ImageLoader.getInstance().displayImage(uri, iv_softImage,
				MyDisplayImageOptions.getInstance());
		if (activeSoft.getStar() != null) {
			float fstar = activeSoft.getStar();
			rb_star.setRating(fstar / 2);
		}
		OnClickListener ll_listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(CollectionActivity.this,
						DetailActivity.class);

				intent.putExtra("softId", activeSoft.getSoftId());
				intent.putExtra("return", 1);
				CollectionActivity.this.startActivityForResult(intent, 10);
				finish();
			}
		};

		linearLayout.setOnClickListener(ll_listener);

		/**
		 * 加载一些列下载有关的东西
		 */
		if (progress == 1) {
			new FoundDown(CollectionActivity.this, linearLayout, activeSoft);
		} else {
			btn_download.setVisibility(View.GONE);
		}
		return linearLayout;

	}

	public LinearLayout dyAddView2(int i, final ActiveSoft activeSoft,
			int progress) {
		LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
				R.layout.collection_item, null);
		ImageView iv_softImage = (ImageView) linearLayout
				.findViewById(R.id.iv_softImage);
		TextView tv_softName = (TextView) linearLayout
				.findViewById(R.id.tv_softName);
		RatingBar rb_star = (RatingBar) linearLayout.findViewById(R.id.rb_star);
		TextView tv_softSize = (TextView) linearLayout
				.findViewById(R.id.tv_softSize);
		Button btn_download = (Button) linearLayout
				.findViewById(R.id.btn_download);
		if (activeSoft.getName() != null) {
			tv_softName.setText(activeSoft.getName());
		}
		if (activeSoft.getSize() != null) {
			tv_softSize.setText(Float.toString(activeSoft.getSize()) + "MB");
		}
		String uri = VMConst.URL + activeSoft.getImageUrl();
		ImageLoader.getInstance().displayImage(uri, iv_softImage,
				MyDisplayImageOptions.getInstance());
		if (activeSoft.getStar() != null) {
			float fstar = activeSoft.getStar();
			rb_star.setRating(fstar / 2);
		}
		btn_download.setBackgroundResource(R.drawable.installed_border);
		btn_download.setTextColor(this.getResources().getColor(
				R.color.installed_color));
		btn_download.setText("已安装");
		OnClickListener ll_listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(CollectionActivity.this,
						DetailActivity.class);

				intent.putExtra("softId", activeSoft.getSoftId());
				intent.putExtra("return", 1);
				CollectionActivity.this.startActivityForResult(intent, 10);
				finish();
			}
		};

		linearLayout.setOnClickListener(ll_listener);
		return linearLayout;

	}
}